//
//  ICMHeaderView.m
//  ICrEAM
//
//  Created by rainimute on 16/10/31.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMHeaderView.h"

@interface ICMHeaderView ()

@property (nonatomic, strong) NSArray *titles;

@property (nonatomic, strong) NSMutableArray *titleButtons;

@property (nonatomic, strong) NSMutableArray *titleLables;

@property (nonatomic, strong) NSString *type;

@end

@implementation ICMHeaderView

- (instancetype)initHeaderViewWithFrame:(CGRect)frame andTitles:(NSArray*)titles andType:(NSString *)type{
    if (self = [super initWithFrame:frame]) {
        self.titles = titles;
        self.type = type;
        for (UIButton *titleButton in self.titleButtons) {
            titleButton.selected = NO;
            [self addSubview:titleButton];
        }
        self.selectedIndex = 0;
    }
    return self;
}

- (void)setSelectedIndex:(NSInteger)selectedIndex{
    _selectedIndex = selectedIndex;
    if (_selectedIndex>self.titles.count) {
        return;
    }
    for (NSInteger i=0; i<self.titles.count; i++) {
        UIButton *titleButton = self.titleButtons[i];
        UILabel *titleLabel = self.titleLables[i];
        titleButton.selected = i==selectedIndex;
        titleLabel.backgroundColor = i==selectedIndex?TitleColor:[UIColor clearColor];
    }
}

+ (instancetype)createHeaderViewWithFrame:(CGRect)frame andTitles:(NSArray*)titles andType:(NSString *)type{
    return [[ICMHeaderView alloc]initHeaderViewWithFrame:frame andTitles:titles andType:type];
}

- (NSMutableArray *)titleButtons{
    if (!_titleButtons) {
        _titleButtons = [NSMutableArray array];
        for (NSInteger i=0; i<self.titles.count; i++) {
            
            UIButton *titleButton = [[UIButton alloc]initWithFrame:CGRectMake([self.type isEqualToString:@"1"] ? i*(ScreenWidth/8) : i*(self.width/self.titles.count), 0, [self.type isEqualToString:@"1"] ? ScreenWidth/6 : self.width/self.titles.count, self.height)];
            [titleButton setUpButtonWithNormolTitle:self.titles[i] selectedTitle:self.titles[i] normolTitleSize:16 selectedTitleSize:16 normolTitleColor:ContentTextColor_Black selectedTitleColor:TitleColor normolBcakImage:nil selectedBackImage:nil actionSEL:@selector(titleButtonAction:) target:self];
            [titleButton addSubview:self.titleLables[i]];
            [_titleButtons addObject:titleButton];
        }
    }
    return _titleButtons;
}

- (void)titleButtonAction:(UIButton*)sender{
    NSInteger index = [self.titleButtons indexOfObject:sender];
    self.selectedIndex = index;
    if ([self.delegate respondsToSelector:@selector(headerViewDidSelectedButtonAtIndex:)]) {
        [self.delegate headerViewDidSelectedButtonAtIndex:index];
    }
}

- (NSMutableArray *)titleLables{
    if (!_titleLables) {
        _titleLables = [NSMutableArray array];
        for (NSInteger i=0; i<self.titles.count; i++) {
            UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.height-1, [self.type isEqualToString:@"1"] ? ScreenWidth/6:self.width/self.titles.count, 1)];
            titleLabel.backgroundColor = TitleColor;
            [_titleLables addObject:titleLabel];
        }
    }
    return _titleLables;
}

@end






